package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Activity;
import com.daka.entity.Group;
import com.daka.entity.User;
import com.daka.enumerate.StatusCode;
import com.daka.service.ActivityService;
import com.daka.vo.request.ActivityPageRequestVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import com.daka.vo.response.PageListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/activity")
@Slf4j
public class ActivityController {

    @Resource(name = "activityService")
    private ActivityService activityService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<Activity> list() {

        List<Activity> topicList = activityService.selectAll();
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/listByGroup", consumes = "application/json")
    @Login
    public ListDataResponseVo<Activity> listByOrg(@Valid @RequestBody Group group) {

        List<Activity> topicList = activityService.selectByGroup(group.getId());
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/pageListByGroup", consumes = "application/json")
    @Login
    public DataResponseVo<PageListResponse<Activity>> pageListByGroup(@Valid @RequestBody ActivityPageRequestVo activityPageRequestVo) {

        List<Activity> activityList = activityService.selectPageByGroup(activityPageRequestVo.getGroupId(), activityPageRequestVo.getActivityName(), (activityPageRequestVo.getPageIndex() - 1) * activityPageRequestVo.getPageSize(), activityPageRequestVo.getPageSize());
        int count = activityService.selectCountByGroup(activityPageRequestVo.getGroupId(), activityPageRequestVo.getActivityName());
        PageListResponse<Activity> page = new PageListResponse<>(activityList, count);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), page);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/listByUser", consumes = "application/json")
    @Login
    public ListDataResponseVo<Activity> listByUser(@Valid @RequestBody User user) {

        List<Activity> topicList = activityService.selectByUser(user.getUserId());
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/info", consumes = "application/json")
    @Login
    public DataResponseVo<Activity> info(@Valid @RequestBody Group group) {
        Activity result = activityService.selectById(group.getId());

        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }
}
